package com.rljk.controller.app;

import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;
import com.rljk.service.CategoryManager;
import com.rljk.service.ProductManager;
import com.rljk.utils.Tools;
import com.rljk.utils.ToolsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:   YASUO
 * Date:     2018/6/25 15:36
 * Description: oto页面接口
 */
@RestController
@RequestMapping("/oto")
@Slf4j
public class OtoInController extends BaseController{

    @Resource(name = "categoryService")
    private CategoryManager categoryService;

    @Resource(name = "productService")
    private ProductManager productService;

    /**
     * OTO首页接口
     * author:YASUO
     * date:2018/6/25
     */
    @RequestMapping(value="/showview")
    public Object showview() {
        log.info("OTO首页显示接口");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("OTOSHOWVIEW", pd.getString("FKEY"))) {
                //if (AppUtil.checkParam("otoshowview",pd)){
                    List<PageData> categoryList = categoryService.listAll(pd);
                    for (PageData p : categoryList) {
                        p.remove("IS_SHOW");
                        p.remove("SEQUENCE");
                        p.remove("CREATE_TIME");
                        p.remove("UPDATE_TIME");
                        p.remove("PARENT_ID");
                    }
                    map.put("categoryList",categoryList);

                    if(Tools.isEmpty(pd.getString("CATEGORY_ID"))){
                        //默认显示全部商品页面
                        pd.put("CATEGORY_ID","461159728450371584");
                    }
                    List<PageData> productList = new ArrayList<>();
                    List<PageData> proList = productService.listAll(pd);
                    for (PageData p : proList) {
                        PageData w = new PageData();
                        w.put("PRODUCT_ID",p.get("PRODUCT_ID"));
                        ToolsUtil.putImgstoArray(p,"MAIN_IMAGE");
                        w.put("MAIN_IMAGE",p.get("MAIN_IMAGE"));
                        w.put("MAIN_IMAGE_TYPE",p.get("MAIN_IMAGE_TYPE"));
                        productList.add(w);
                    }
                    map.put("productList",productList);
                    return Result.success(map);
               /* }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }*/
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * app兑换中心获取商品
     * author:YASUO
     * date:2018/7/12
     */
    @RequestMapping(value="/getexchangeprod")
    public Object getExchangeProd() {
        log.info("app兑换中心获取商品");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("EXCHANGEPRODUCT", pd.getString("FKEY"))) {
                //if (AppUtil.checkParam("otoshowview",pd)){
                List<PageData> list = productService.getExchangeProd(pd);
                map.put("list",list);
                return Result.success(map);
               /* }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }*/
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * app幸运大转盘抽奖
     * author:YASUO
     * date:2018/7/12
     */
    @RequestMapping(value="/luckdraw")
    public Object luckdraw() {
        log.info("app幸运大转盘抽奖");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("LUCKYDRAW", pd.getString("FKEY"))) {
                //if (AppUtil.checkParam("otoshowview",pd)){
                List<PageData> list = productService.getExchangeProd(pd);
                map.put("list",list);
                return Result.success(map);
               /* }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }*/
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


}
